using Domain;
using ErrorCode;
using MediatR;
using SqlSugar;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WriteAPI.Command.生产计划;

namespace WriteAPI.CommandHandle.生产计划
{
    /// <summary>
    /// 获取生产计划详情命令处理程序
    /// </summary>
    public class GetProductionPlanDetailCommandHandler : IRequestHandler<GetProductionPlanDetailCommand, APIResult<ProductionPlanDetailViewModel>>
    {
        private readonly ISqlSugarClient _db;

        public GetProductionPlanDetailCommandHandler(ISqlSugarClient db)
        {
            _db = db;
        }

        public async Task<APIResult<ProductionPlanDetailViewModel>> Handle(GetProductionPlanDetailCommand request, CancellationToken cancellationToken)
        {
            var result = new APIResult<ProductionPlanDetailViewModel>();

            try
            {
                // 获取生产计划主数据
                var plan = await _db.Queryable<Lwp_ProductionPlan>()
                    .FirstAsync(p => p.Id == request.Id);

                if (plan == null)
                {
                    result.Code = 404;
                    result.Message = "生产计划不存在";
                    return result;
                }

                // 获取生产计划明细数据
                var items = await _db.Queryable<ProductionPlanDetail>()
                    .Where(d => d.PlanId == request.Id)
                    .ToListAsync();

                // 获取生产计划附件数据
                var attachments = await _db.Queryable<ProductionPlanAttachment>()
                    .Where(a => a.PlanId == request.Id)
                    .ToListAsync();

                // 构建详情视图模型
                var viewModel = new ProductionPlanDetailViewModel
                {
                    Id = plan.Id,
                    PlanNumber = plan.DocNumber,
                    PlanTheme = plan.VersionDesc,
                    PlanDate = plan.DocDate,
                    SalesOrder = plan.BizNumber,
                    RelatedProject = plan.BizUser,
                    UnscheduledOrder = string.IsNullOrEmpty(plan.BizNumber),
                    Remarks = plan.Remark,
                    Items = items.Select(item => new ProductionPlanDetailItem
                    {
                        Id = item.Id,
                        ItemCode = item.ItemCode,
                        ItemName = item.ItemName,
                        Spec = item.Spec,
                        Unit = item.Unit,
                        ItemCategory = item.ItemCategory,
                        ItemClass = item.ItemClass,
                        Brand = item.Brand,
                        Quantity = item.Quantity,
                        DetailDate = item.DetailDate,
                        MainUnitQuantity = item.MainUnitQuantity,
                        Remark = item.Remark
                    }).ToList(),
                    Attachments = attachments.Select(attach => new ProductionPlanDetailAttachment
                    {
                        Id = attach.Id,
                        FileName = attach.FileName,
                        FilePath = attach.FilePath,
                        FileSize = attach.FileSize,
                        FileType = attach.FileType,
                        UploadTime = attach.UploadTime
                    }).ToList()
                };

                result.Data = viewModel;
                result.Success = true;
                result.Code = 200;
                result.Message = "获取生产计划详情成功";
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Code = 500;
                result.Message = $"获取生产计划详情失败: {ex.Message}";
            }

            return result;
        }
    }
} 